# coding=utf-8
# author:xc
# datetime:2018/8/1 14:10


from flask import render_template, request
from app import util, common
from . import admin
from dbConnection.MysqlConn import Mysql


@admin.route('/admin/machine/list.html', methods=['POST', 'GET'])
@util.admin_login
def admin_machine_list():
    if request.method == 'GET':
        storeId = request.args.get('storeId')
        page = request.args.get('page', default=1)
        machineNum = request.args.get('machineNum')
        page = int(page)
        num = (page - 1) * util.PAGE_NUM
        mysql = Mysql()
        sql = "select * from car_machine where storeId='%s'" % storeId
        sql2 = "select count(*) from car_machine where storeId='%s' " % storeId
        if machineNum != "" and machineNum is not None:
            sql += " and machineNum='%s'" % machineNum
            sql2 += " and machineNum='%s'" % machineNum
        sql += " order by addTime desc  limit %d,%d " % (num, util.PAGE_NUM)
        result = mysql.getAll(sql, None)
        count = mysql.getOne(sql2, None)
        count = int(count['count(*)'])
        code = util.CODE_EMPTY
        data = []
        if count % util.PAGE_NUM > 0:
            max_page = count / util.PAGE_NUM + 1
        else:
            max_page = count / util.PAGE_NUM
        if result:
            code = util.CODE_SUCCESS
            data = result
        mysql.dispose()
        res = {
            'data': data,
            'page': page,
            'max_page': max_page,
            'code': code,
        }
        return render_template('admin/store/machine_list.html', res=res, storeId=storeId)


@admin.route('/admin/machine/edit.html', methods=['POST', 'GET'])
@util.admin_login
def admin_machine_edit():
    if request.method == "GET":
        machineId = request.args.get('machineId')
        mysql = Mysql()
        sql = "select * from car_machine where machineId='%s'" % machineId
        result = mysql.getOne(sql, None)
        mysql.dispose()
        result['num1'] = result['machineNum'][0:8]
        result['num2'] = result['machineNum'][8:16]

        return render_template('admin/store/machine_edit.html', result=result)
    else:
        try:
            machineNum = request.values.get('machineNum')
            machineId = request.values.get('machineId')
            print machineNum,machineId
            mysql = Mysql()
            res = mysql.getOne("select * from car_machine where machineNum='%s'" % machineNum, None)
            code = util.CODE_EMPTY
            print res
            if res is None:
                sql = "update car_machine set machineNum='%s' where machineId='%s'" % (machineNum, machineId)
                result = mysql.update(sql, None)
                if result > 0:
                    code = util.CODE_SUCCESS
                    common.set_machine_conf()
            mysql.dispose()
        except Exception as e:
            print e
            code = util.CODE_ERROR
        data = {
            'code': code
        }
        return util.to_json(data)


@admin.route('/admin/machine/stop', methods=['POST'])
@util.admin_login
def admin_machine_stop():
    machineId = request.values.get('machineId')
    mysql = Mysql()
    sql = "update car_machine set status=0 where machineId='%s'" % machineId
    res = mysql.update(sql, None)
    mysql.dispose()
    if res > 0:
        common.set_machine_conf()
        data = {
            'status': 1
        }
    else:
        data = {
            'status': 0
        }
    return util.to_json(data)


@admin.route('/admin/machine/start', methods=['POST'])
@util.admin_login
def admin_machine_start():
    machineId = request.values.get('machineId')
    mysql = Mysql()
    sql = "update car_machine set status=1 where machineId='%s'" % machineId
    res = mysql.update(sql, None)
    mysql.dispose()
    if res > 0:
        common.set_machine_conf()
        data = {
            'status': 1
        }
    else:
        data = {
            'status': 0
        }
    return util.to_json(data)


@admin.route('/admin/machine/delete', methods=['POST'])
@util.admin_login
def admin_machine_delete():
    machineId = request.values.get('machineId')
    mysql = Mysql()
    sql = "delete from car_machine  where machineId='%s' " % machineId
    res = mysql.delete(sql, None)
    mysql.dispose()
    if res > 0:
        common.set_machine_conf()
        data = {
            'status': 1
        }
    else:
        data = {
            'status': 0
        }
    return util.to_json(data)


@admin.route('/admin/machine/newly.html', methods=['POST', 'GET'])
@util.admin_login
def admin_machine_newly():
    if request.method == 'GET':
        storeId = request.args.get('storeId')
        return render_template('admin/store/machine_newly.html', storeId=storeId)
    else:
        try:
            machineNum = request.values.get('machineNum')
            storeId = request.values.get('storeId')
            mysql = Mysql()
            code = util.CODE_EMPTY
            res = mysql.getOne("select * from car_machine where machineNum='%s'" % machineNum, None)
            if res is None:
                sql = "insert into car_machine(machineNum,addTime,storeId) values('%s',now(),'%s')" % (
                    machineNum, storeId)
                result = mysql.insertOne(sql, None)
                if result > 0:
                    common.set_machine_conf()
                    code = util.CODE_SUCCESS
            mysql.dispose()
        except Exception as e:
            print e
            code = util.CODE_ERROR
        data = {
            'code': code
        }
        return util.to_json(data)
